<?php

/**
* Class that manages the transaltion
*/
class BabiliTranslation{

	
	//the db connector
	private $db;
	
	/**
	 * The construct load the db connector
	 * @param $db
	 */
	public function __construct($db){
		$this->db = $db;
	}
		
	/*
	* Function that add a transaltion
	* @param $value
	* @param $property_key_id
	* @param $language_id , id of the language
	* @param $contributor_id user id of the contributor
	* @param $validator_id user id of the validator
	* @param $contributor_comment the comment of the contributor
	* @return bool true if successful, false else
	*/
	public function add($value, $property_key_id, $language_id,  $contributor_id, $validator_id, $contributor_comment){
		if (empty($value) || !is_numeric($property_key_id) || empty($contributor_comment) || !is_numeric($validator_id) || !is_numeric($contributor_id)){
			return false;
		}
		
		$query = "INSERT into translation 
				  (value, property_key_id, language_id, status, contributor_id, 
				  validator_id, contributor_comment, validator_comment, date_modified)		
				  VALUES (:value, :property_key_id, :language_id, :status, :contributor_id, :validator_id, :contributor_comment, :validator_comment,  NOW());";
		
		$query_args = array(':value' => $value,
							':property_key_id' => $property_key_id,
							':language_id' => $language_id,
							':status' => "empty",
							':contributor_id' => $contributor_id,
							':validator_id' => $validator_id,
							':validator_comment' => "", 
							':contributor_comment' => $contributor_comment);
		
		$this->db->update($query, $query_args);
		return $this->db->last_id();
	}
	
	/*
	 * Function that modify a translation
	 * @param $id the id of the translation
	 * @param $value
	 * @param $contributor_id user id of the contributor
	 * @param $validator_id user id of the validator
	 * @param $contributor_comment the comment of the contributor
	 * @param $validator_comment the comment of the validator
	 * @return bool true if successful, false else
	 */
	public function modify($id, $value, $contributor_id, $validator_id, $contributor_comment, $validator_comment){
	
		$id = intval($id);
		
//		var_dump($id);
//		var_dump($value);
//		var_dump($contributor_id);
//		var_dump($validator_id);
//		var_dump($contributor_comment);
//		var_dump($validator_comment);
		
		if (!is_numeric($id) || empty($value) || !is_numeric($contributor_id) 
			|| empty($contributor_comment) || !is_numeric($validator_id)  || empty($validator_comment)){

				return false;

			}
			
		$query = "UPDATE translation 
				  SET value = :value, contributor_id = :contributor_id, validator_id = :validator_id, 
				  contributor_comment = :contributor_comment, validator_comment = :validator_comment, date_modified = NOW()
				  WHERE id = :id";
		
//		var_dump($query);
		
		$query_args = array(':id' => $id,
							':value' => $value,
						   ':contributor_id' => $contributor_id,
						    ':validator_id' => $validator_id, 
							':contributor_comment' => $contributor_comment,
							':validator_comment' => $validator_comment);
			
		return $this->db->update($query, $query_args);
		
	}
	
	/*
	 * Function to get a translation
	 * @param $id
	 * @return array $translation = array('id' => $id,
	 * 									  'value' => $value,
	 * 				 					  'status' => $status,
	 * 				  					  'property_key' => array('name' => $property_name, 'comment' => $property_comment, '$fileid' => $property_fileid),
	 * 				 					  'project' => array('name' => $projectname, 'label' => $projectversion, 'status' => $status),
	 * 				 					  'language' => array('name' => $name, 'label' => $label, 'country_code' => $code, 'language_code' => $language_code, 'flag' => $flag),
	 *				 					  'validator' => array('id' => $userid, 'email' => $email),
	 * 				 					  'contributor' => array('id' => $userid, 'email' => $email),
	 * 				 					  'comment_contributor' = $comment_contributor,
	 * 				 					  'comment_validator' => $comment_validator
	 * 				 )
	 * @return bool true if successful, false else
	*/
	public function get_translation($id){
		$query = "SELECT value, property_key_id, language_id, status, contributor_id, 
					validator_id, contributor_comment, validator_comment, date_modified
					FROM translation WHERE id = :id";
		
		$query_args = array(':id' => $id);
		
		$translation = reset($this->db->select($query, $query_args));
		
		return $translation;
	}
	
	/*
	 * Function that get all translation of a property key
	 * @param $property_key_id
	 * @param $language_ids array of language ids [optional, if you want to limit to some languages]
	 * @return array of $translation (see the function get)
	 */
	public function get_all_translation($property_key_id, $language_ids){
		
		$query = "SELECT id, value, property_key_id, language_id, status, contributor_id, 
					validator_id, contributor_comment, validator_comment, date_modified
				 	FROM translation
				 	WHERE property_key_id = :property_key_id ";
		
		$query_args[':property_key_id'] = $property_key_id;
		
		if(!empty($language_ids)){
			$query .= " AND language_id IN (";
			foreach($language_ids as $language_id){
				$query .= ":" . $language_id . ',';
				$query_args[":" . $language_id] = $language_id;
			}
			//replace the last comma by a parentheses
			$query = substr_replace($query, ')', -1);
		}
		
		$result = $this->db->select($query, $query_args);
//		echo "A REVOIR !! (die)";
//		var_dump($result); die();
		return $result;
		
	}
	
	/*
	 * Function that get all property keys of a project
	 * @param $projectid
	 * @param $language_ids array of language ids [optional, if you want to limit translations to some languages]
	 * @return array($property_key_id => array('id', 
	 * 											'name',
	 * 										   'comment',
	 * 										   'fileid',
	 * 										   'max_length',
	 * 										   'translations' => array($tranlsation)))
	 */
	public function get_all_property_keys($projectid, $language_ids){
		$projectid = intval($projectid);

		if (!is_numeric($projectid) || empty($language_ids)){
			return NULL;
		}
		// select all entries in the table "file" for project_id
		$query = "SELECT prop_key.id as id, prop_key.name as name, prop_key.comment as comment, prop_key.file_id as file_id, prop_key.max_length as max_length 
				  FROM file as f
				  INNER JOIN property_key as prop_key ON prop_key.file_id = f.id
				  WHERE f.project_id = :projectid";
		
		$query_args = array(':projectid' => $projectid);

		$result = $this->db->select($query, $query_args);
		
		$property_keys = array();
		
		foreach($result as $key => $value){
			$property_keys[$value['id']] = $value;
			$property_keys[$value['id']]['translations'] = $this->get_all_translation($value['id'], $language_ids);
		} 
		
		return $property_keys;
		
	}
	
	/*
	 * Function to add File to a project
	 * @param $path a string with the path of the file
	 * @param $projectid
	 * @return bool true if successful, false else
	*/
	public function add_file($path, $projectid){
		
		if (empty($path) || empty($projectid)){
			return false;
		}
		
		// insert new values in the table "file"
		$query = "INSERT into file (path, project_id) VALUES (:path, :project_id)";
		$query_args = array(':path' => $path,
							':project_id' => $projectid);
		if ($this->db->update($query, $query_args)){
			return $this->db->last_id();	
		}
		
	}
	
	/*
	 * Function to add property key to a project
	 * @param $name
	 * @param $comment (to detail the context)
	 * @param $fileid
	 * @return bool true if successful, false else
	*/
	public function add_property_key($name, $comment, $fileid){
		
		if (empty($name) || empty($comment) || !is_numeric($fileid)){
			return false;		
		}
			// insert new values in the table "property_key"
		$query = "INSERT into property_key (name, comment, file_id) VALUES (:name, :comment, :file_id)";
		$query_args = array(':name' => $name,
							':comment' => $comment,
							':file_id' => $fileid);
		if ($this->db->update($query, $query_args)){
			return $this->db->last_id();	
		}		
	}
}